from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import requests
# 打开浏览器
chrome = webdriver.Chrome()
chrome.get('https://www.acwing.com')
chrome.maximize_window()

# 账号和密码
account = "1428206861@qq.com"
password = "wwwl192837465"

try:
    sleep(1)
    # 等待并点击 "登录/注册" 按钮
    login_trigger = WebDriverWait(chrome, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//ul[@class="nav navbar-nav navbar-right"]/li[3]/a'))
    )
    login_trigger.click()

    # 等待账号输入框加载并可见
    account_tag = WebDriverWait(chrome, 10).until(
        EC.visibility_of_element_located((By.ID, 'login_username'))
    )
    account_tag.send_keys(account)

    # 等待密码输入框加载并可见,回车登录
    password_tag = WebDriverWait(chrome, 10).until(
        EC.visibility_of_element_located((By.ID, 'login_password'))
    )
    password_tag.send_keys(password)
    sleep(1)
    password_tag.send_keys(Keys.ENTER)
    sleep(2)
    # 下滑到 1000 的位置
    js = 'document.documentElement.scrollTop=1000'
    chrome.execute_script(js)
    sleep(1)

    # 等待并点击头像，出现下拉框
    drop_down_box = WebDriverWait(chrome, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, '#topNavBar > ul.nav.navbar-nav.navbar-right > li:nth-child(5) > a'))
    )
    drop_down_box.click()
    sleep(1)

    # 等待我的空间可见，并点击
    my_room = WebDriverWait(chrome, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '#topNavBar > ul.nav.navbar-nav.navbar-right > li.open > ul > li:nth-child(3) > a'))
    )
    # 创建 ActionChains 对象
    actions = ActionChains(chrome)

    # 将鼠标悬停在目标元素上
    actions.move_to_element(my_room).perform()
    sleep(1)
    my_room.click()
    print(chrome.current_url,'*'*50)

    #双击应用图标
    app = WebDriverWait(chrome, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, '#acwing_body > div:nth-child(29) > div:nth-child(3) > div.file-explorer-main-field-item-icon > img'))
    )
    actions.move_to_element(app).perform()
    sleep(1)
    actions.move_to_element(app).double_click().perform()
    print("双击应用图标成功")

    #等待出现文件资源管理器，双击 AC Saber.app
    WebDriverWait(chrome, 3).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '#acwing_body > div.panel.panel-default.fs-gui-window.ui-draggable.ui-resizable.fs-gui-window-focus'))
    )
    print("成功捕获弹出窗口")

    saber = WebDriverWait(chrome, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[starts-with(@id,"file_explorer_")]/div[2]/div[2]/div[1]'))
    )
    actions.move_to_element(saber).perform()
    sleep(1)
    actions.move_to_element(saber).double_click().perform()
    print("双击AC Saber.app 成功")

    # 等待AC Saber.app运行并出现后悬停在训练模式，并点击
    exercise = WebDriverWait(chrome,10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '[id^="ac_saber_"] > div > div > div.ac-saber-menu-list-item.ac-saber-menu-list-item-training'))
    )
    actions.move_to_element(exercise).perform()
    sleep(1)
    exercise.click()
    print("成功进入训练模式")

    # 等待目录出现，点击基础知识
    base_knowledge = WebDriverWait(chrome, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '[id^="ac_saber_"] > div > div.ac-saber-menu-training-body > div.ac-saber-menu-training-body-chapters-field > div > div.ac-saber-menu-training-body-chapters-item.ac-saber-menu-training-body-chapters-item-basic'))
    )
    actions.move_to_element(base_knowledge).perform()
    sleep(1)
    base_knowledge.click()
    print("成功点击基础知识")

    #等待归并排序出现，并点击
    merge_sort = WebDriverWait(chrome, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '[id^="ac_saber_"] > div > div.ac-saber-menu-training-body > div.ac-saber-menu-training-body-sessions-field > div:nth-child(1) > div:nth-child(2)'))
    )
    actions.move_to_element(merge_sort).perform()
    sleep(1)
    merge_sort.click()
    print("成功点击归并排序")

    # 等待题目出现，并点击确认
    question = WebDriverWait(chrome,10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '[id^="ac_saber_"] > g > g.nodes > g:nth-child(1) > circle'))
    )
    actions.move_to_element(question).perform()
    sleep(1)
    question.click()
    print("成功点击第一道题目")

    #等待开始
    sleep(5)

    a = WebDriverWait(chrome,10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR,'[id^=ac_saber_editor_] > div.ace_scroller > div'))
    )


    # 点击提交答案
    # submit_button = WebDriverWait(chrome,10).until(
    #     EC.element_to_be_clickable((By.CSS_SELECTOR, '[id^="ac_saber_"] > div > div.ac-saber-menu-training-problem-body > div.ac-saber-menu-training-problem-body-main > div.ac-saber-menu-training-problem-body-main-footer > button.btn.btn-success.ac-saber-submit-code-btn'))
    # )
    # actions.move_to_element(submit_button).perform()
    # sleep(1)
    # submit_button.click()
    # print("提交成功")

    # 保留页面一段时间以便观察结果
    # WebDriverWait(chrome, 600).until(
    #     EC.url_changes(chrome.current_url)
    # )
    sleep(600)

except Exception as e:
    print("发生错误：", e)

finally:
    # 关闭浏览器
    chrome.quit()
